/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.Whfa;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.service.GsglService;
import me.zhengjie.business.service.WhfaService;
import me.zhengjie.business.service.dto.OrderQueryCriteria;
import me.zhengjie.business.service.dto.WhfaQueryCriteria;
import me.zhengjie.pay.domain.PaidQueryCriteria;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2022-07-18
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "维护方案管理")
@RequestMapping("/api/whfa")
public class WhfaController {

    private final WhfaService whfaService;
    private final GsglService gsglService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportWhfa(HttpServletResponse response, WhfaQueryCriteria criteria) throws IOException {
        whfaService.download(whfaService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询维护方案")
    @ApiOperation("查询维护方案")
    @PreAuthorize("@el.check('whfa:list')")
    public ResponseEntity<Object> queryWhfa(WhfaQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(whfaService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增维护方案")
    @ApiOperation("新增维护方案")
    @PreAuthorize("@el.check('whfa:add')")
    public ResponseEntity<Object> createWhfa(@Validated @RequestBody Whfa resources){
        return new ResponseEntity<>(whfaService.create(resources),HttpStatus.CREATED);
    }
    @PostMapping(value = "/createZhfa")
    @Log("移动端新增照护方案")
    @ApiOperation("移动端新增照护方案")
    @PreAuthorize("@el.check('whfa:add')")
    public ResponseEntity<Object> createZhfa(@Validated @RequestBody Whfa whfa){

        return new ResponseEntity<>(whfaService.createZhfa(whfa), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改维护方案")
    @ApiOperation("修改维护方案")
    @PreAuthorize("@el.check('whfa:edit')")
    public ResponseEntity<Object> updateWhfa(@Validated @RequestBody Whfa resources){
        whfaService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除维护方案")
    @ApiOperation("删除维护方案")
    @PreAuthorize("@el.check('whfa:del')")
    public ResponseEntity<Object> deleteWhfa(@RequestBody String[] ids) {
        whfaService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("服务计划")
    @GetMapping(value = "/queryFwjhVo")
    public ResponseEntity<Object> queryFwjhVo(WhfaQueryCriteria criteria, Pageable pageable) {
        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();
        log.info("userDetails:{}", userDetails);

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("roleString:{}", roleString);
        if(roleString.indexOf("居家照护") != -1|| roleString.indexOf("居家机构双角色") != -1) {
            log.info("角色：居家照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setAkb020(gsgl.getBisGsglYljgdm());
        } else if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setSbgs(gsgl.getBisGsglId());
        }
        return new ResponseEntity<>(whfaService.queryFwjhVo(criteria, pageable), HttpStatus.OK);
    }

    @Log("导出订单")
    @ApiOperation("导出订单")
    @GetMapping(value = "/queryFwjhVo/download")
    public void exportAllOrderList(HttpServletResponse response, WhfaQueryCriteria criteria) throws IOException {
        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();
        log.info("userDetails:{}", userDetails);

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("roleString:{}", roleString);
        if(roleString.indexOf("居家照护") != -1|| roleString.indexOf("居家机构双角色") != -1) {
            log.info("角色：居家照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setAkb020(gsgl.getBisGsglYljgdm());
        } else if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setSbgs(gsgl.getBisGsglId());
        }
        whfaService.downloadAllFwjhVo(whfaService.queryAllFwjhVo(criteria), response);
    }
}